package com.app.mapper;

import com.app.model.PaymentOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * <p>
 * 订单记录 Mapper 接口
 * </p>
 *
 * @author yoominic
 * @since 2025-03-18
 */
public interface PaymentOrderMapper extends BaseMapper<PaymentOrder> {

    IPage<PaymentOrder> appletGetLoginUserPaymentOrderLog(Page<PaymentOrder> page, @Param("userId") Integer userId,@Param("type") String type);


    IPage<PaymentOrder> adminGetPaymentOrderLogList(Page<PaymentOrder> page,@Param("orderType")Integer orderType,@Param("payStatus")Integer payStatus,
                                                    @Param("invoiceStatus")Integer invoiceStatus,@Param("receiptStatus")Integer receiptStatus,@Param("houseType")Integer houseType,
                                                    @Param("fwkeyword")String fwkeyword,@Param("yhkeyword")String yhkeyword,
                                                    @Param("payTime")String payTime,@Param("untilPayTime")String untilPayTime,
                                                    @Param("orderPayStartTime")String orderPayStartTime,@Param("orderPayEndTime")String orderPayEndTime);

    @Select("SELECT \n" +
            "    COALESCE(SUM(po.total_payable_amount), 0) AS total_payable_amount,\n" +
            "    COALESCE(SUM(po.total_payment_amount), 0) AS total_payment_amount,\n" +
            "    COALESCE(SUM(CASE WHEN po.payment_type = 1 THEN po.total_payment_amount END), 0) AS total_payment_amount_type_1,\n" +
            "    COALESCE(SUM(CASE WHEN po.payment_type != 1 THEN po.total_payment_amount END), 0) AS total_payment_amount_not_type_1\n" +
            "FROM tb_payment_order po\n" +
            "where 1=1 and po.pay_status = 1")
    Map<String,Object> getPaymentOrderTopStatistics();


    @Select("SELECT \n" +
            "    COALESCE(SUM(rent), 0) AS totalRent,\n" +
            "    COALESCE(SUM(deposit_paid), 0) AS totalDepositPaid,\n" +
            "    COALESCE(SUM(late_fee), 0) AS totalLateFee,\n" +
            "    COALESCE(SUM(property_fee), 0) AS totalPropertyFee\n" +
            "FROM  tb_payment_order\n" +
            "WHERE 1 = 1 AND user_id = #{userId} AND pay_status = 1")
    Map<String, Object> getPaymentOrderSumInfoByUserId(@Param("userId") Integer userId);

}
